package servers;
import java.io.*;
import java.util.*;


public class Dragon {

	/**
	 * @param args
	 */
	private static BufferedWriter log;
	private static utilities.DragonConfig dc;

	public static void delgator(utilities.ProcFilter pf){
		delgator(pf,null);
	}

	public static void delgator(utilities.ProcFilter pf, String vars){
				
		utilities.EngineCommander wc = (utilities.EngineCommander) dc.getCommanders().get(pf.getEngine());
		if(vars!=null)
		{
			try {
				log.write(wc.getName() + " using custom: " + vars + "\n");
				pf.setResource(vars);		
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			try {
				log.write(wc.getName() + " using default " + pf.getResource() + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//split string on , makd arg array		
		String sargs[] = pf.getResource().split(",");				
		utilities.Action a = new 
				utilities.Action(sargs, wc.getActionDefByName(pf.getActionString()));		
		
		wc.addAction(a);	
		//System.out.println("delegator added action to : " + wc.getName());		
	}

	public static void logToServer(String message){
		
		utilities.EngineCommander wc = (utilities.EngineCommander) dc.getCommanders().get("Sender Engine");
		
		//split string on , makd arg array		
		String sargs[] = new String[2];
		sargs[0]="addLog";
		sargs[1]="Dragon says: " + message;
		utilities.Action a = new  utilities.Action(sargs, wc.getActionDefByName("Send"));				
		wc.addAction(a);	
		//System.out.println("delegator added action to : " + wc.getName());		
	}

	
	public static void voiceDelegator(String inPhrase){
		try
		{
			utilities.ProcessorCommander wc = (utilities.ProcessorCommander) dc.getCommanders().get("Voice Processor");
			wc.addKey(inPhrase);			
		}
		catch(Exception e)
		{
			try {
				log.write("error " + e.getMessage() + "\n");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();	
		}
		
	}
	public static void commandDelegator(String command){
		try
		{
		
		utilities.ProcessorCommander wc = (utilities.ProcessorCommander) dc.getCommanders().get("Command Processor");
		wc.addKey(command);
		}
		catch(Exception e)
		{
			try {
				log.write("error " + e.getMessage() + "\n");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();	
		}
	}
	
	public static void sensorDelegator(String inString){
		try
		{
		
			utilities.ProcessorCommander wc = (utilities.ProcessorCommander) dc.getCommanders().get("Sensor Processor");
			wc.addKey(inString);
		}
		catch(Exception e)
		{
			try {
				log.write("error " + e.getMessage() + "\n");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();	
		}
	}	
	public static void programDelegator(String inString){
	try
		{
		
		utilities.ProcessorCommander wc = (utilities.ProcessorCommander) dc.getCommanders().get("Program Processor");
		wc.addKey(inString);
	}
	catch(Exception e)
	{
		try {
			log.write("error " + e.getMessage() + "\n");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		e.printStackTrace();	
	}
	}	
	
	public static void main(String[] args) {

		try
		{
			dc = new utilities.DragonConfig("C:\\workspace\\dragon\\src\\dragonConfig.xml");						
			log = new BufferedWriter(new FileWriter(dc.getLogFile()));			
			log.write("Dragon Started\n");
			System.out.println("Dragon Started");
			//dc doconfig has the engines in it...
						
			/*
			 * main loop keeping threads running
			 */
			Scanner in = new Scanner(System.in);;     
			String tstring;
			//logToServer("Starting...");
			do 
			{
				Thread.sleep(1000);
				tstring = in.nextLine();
				System.out.println(tstring);
				
				//if string = t then test it
				
				if(tstring.equals("t"))
				{
					utilities.EngineCommander wc = (utilities.EngineCommander) dc.getCommanders().get("Wave Commander");
					String pargs[] = new String[1];
					pargs[0]="c:\\waves\\yes-indeed.wav";
					utilities.Action a = new 
							utilities.Action(pargs, wc.getActionDefByName("Play"));										
					wc.addAction(a);	
					System.out.println("action added");
					try
					{
						Dragon.logToServer("Hello");
					}
				catch(Exception le)
					{
						le.printStackTrace();
					}
				}
				if(tstring.equals("s"))
				{
					utilities.EngineCommander wc = (utilities.EngineCommander) dc.getCommanders().get("Roomba Commander");
					String pargs[] = new String[0];
					utilities.Action a = new 
							utilities.Action(pargs, wc.getActionDefByName("GetSensors"));										
					wc.addAction(a);	
					System.out.println("action added");
				}			
				if(tstring.equals("e"))
				{
					utilities.EngineCommander wc = (utilities.EngineCommander) dc.getCommanders().get("Roomba Commander");
					String pargs[] = new String[0];
					utilities.Action a = new 
							utilities.Action(pargs, wc.getActionDefByName("Stop"));										
					wc.addAction(a);	
					System.out.println("stop action added");
				}			
				if(tstring.equals("f"))
				{
					utilities.EngineCommander wc = (utilities.EngineCommander) dc.getCommanders().get("Roomba Commander");
					String pargs[] = new String[0];
					utilities.Action a = new 
							utilities.Action(pargs, wc.getActionDefByName("GoForward"));										
					wc.addAction(a);	
					System.out.println("forward action");
				}			
				if(tstring.equals("r"))
				{
					utilities.EngineCommander wc = (utilities.EngineCommander) dc.getCommanders().get("Roomba Commander");
					String pargs[] = new String[0];
					utilities.Action a = new 
							utilities.Action(pargs, wc.getActionDefByName("GoBackward"));										
					wc.addAction(a);	
					System.out.println("backward action");
				}			
				if(tstring.equals("p"))
				{
					commandDelegator("Get Picture");
				}
				if(tstring.equals("n"))
				{
					utilities.EngineCommander wc = (utilities.EngineCommander) dc.getCommanders().get("Roomba Commander");
					String pargs[] = new String[2];
					pargs[0]="72";
					pargs[1]="10";
					utilities.Action a = new 
							utilities.Action(pargs, wc.getActionDefByName("PlayNote"));										
					wc.addAction(a);	
					System.out.println("action added");
				}				
				
				
			}while(!tstring.equals("exit"));

				
			/*
			 * closing down....
			 * 
			 */
			
			
			//close down  and end
			//iterate commanders, close each one....
			for (Iterator it = dc.getCommanders().values().iterator(); it.hasNext();) {
				utilities.Commander ec = (utilities.Commander) it.next();
				ec.close();
			}			
			System.out.println("Dragon Stopped");
			log.write("Dragon Stopped\n");			
			log.close();
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}

}
